<?php
namespace App\V1;

use Phalcon\Loader,
    Phalcon\Mvc\View,
    \Phalcon\DI\Injectable,
    \Phalcon\DiInterface,
    App\Helpers\Utils;

/**
 * 模块类
 * @package App\V1
 */
class Module extends Injectable {

    /**
     * 模块相关配置
     * @var \Phalcon\Config
     */
    private $config;

    /**
     * 构造函数.
     */
    public function __construct() {
        $this->config = Utils::configItem("module");
    }

    /**
     * 自动加载特定模块的配置
     * @param \Phalcon\DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di) {
        $loader = new Loader();
        $namespaces = $this->config->v1->namespaces->toArray();
        $loader->registerNamespaces($namespaces, true)->register();
    }

    /**
     * 注册该模块特定的服务
     * @param \Phalcon\DiInterface $di
     */
    public function registerApps(DiInterface $di) {
        $v1 = $this->config->v1;
        /**
         * 设置视图组件
         */
        $di->setShared('view', function () use ($v1) {
            $view = new View();
            $view->setViewsDir(isset($v1->views) ? $v1->views : "");
            return $view;
        });
    }
}